package com.framework.service.common.impl;

import com.framework.common.model.response.R;
import com.framework.common.util.other.NumeralUtil;
import com.framework.common.util.pinyin.PinYinUtil;
import com.framework.mapper.common.DistrictMapper;
import com.framework.model.common.vo.DistrictVo;
import com.framework.model.system.SystemUser;
import com.framework.service.base.BaseService;
import com.framework.service.common.DistrictService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 龘鵺
 * @version 1.0
 * @className com.framework.service.common.impl.DistrictServiceImpl
 * @description 地区业务接口实现类
 * @datetime 2024-05-13 11:25:40
 */
@Service("districtServiceImpl")
public class DistrictServiceImpl extends BaseService implements DistrictService {
    private Logger log = LoggerFactory.getLogger(DistrictServiceImpl.class);
    @Autowired
    private DistrictMapper districtMapper;

    /**
     * @param id 1 编号
     * @return com.framework.model.common.vo.DistrictVo
     * @title 根据编号查询地区
     * @description 根据编号查询地区
     * @author 龘鵺
     * @datetime 2024-05-13 11:25:40
     * @version 1.0
     */
    @Override
    public DistrictVo getByIdParam(Long id) {
        DistrictVo row = new DistrictVo();
        row.setId(id);
        row.setGtaeOperaterStatus(NumeralUtil.POSITIVE_ONE);
        DistrictVo p = districtMapper.selectByParam(row);
        if (p == null) {
            return new DistrictVo();
        }
        return p;
    }

    /**
     * @param row 1 地区类
     * @return com.framework.common.model.response.R
     * @title 新增地区
     * @description 新增地区
     * @author 龘鵺
     * @datetime 2024-05-13 11:25:40
     * @version 1.0
     */
    @Override
    @Transactional
    public R<?> save(DistrictVo row) {
        SystemUser systemUser = getUser();
        Long userId = systemUser.getId();
        Date date = new Date();
        row.setCreateId(userId);
        row.setCreateTime(date);
        row.setOperaterId(userId);
        row.setOperaterTime(date);
        row.setOperaterStatus(NumeralUtil.POSITIVE_ONE);
        districtMapper.insertSelective(row);
        return R.ok();
    }

    /**
     * @param row 1 地区类
     * @return com.framework.common.model.response.R
     * @title 编辑地区
     * @description 编辑地区
     * @author 龘鵺
     * @datetime 2024-05-13 11:25:40
     * @version 1.0
     */
    @Override
    @Transactional
    public R<?> edit(DistrictVo row) {
        DistrictVo queryParam = districtMapper.selectByPrimaryKey(row.getId());
        if (queryParam == null) {
            return R.fail("编辑信息不存在!");
        }
        row.setParentCode(null);
        row.setCode(null);
        row.setLevel(null);
        Map<String, String> map = PinYinUtil.toPinYinMap(row.getName());
        if (map.size() > NumeralUtil.POSITIVE_ZERO) {
            row.setPinYin(map.get("pinYinStr"));
            row.setInitial(map.get("initialStr"));
            row.setPinYinAcronym(map.get("pinYinAcronymStr"));
        }
        SystemUser systemUser = getUser();
        Long userId = systemUser.getId();
        Date date = new Date();
        row.setOperaterTime(date);
        row.setOperaterId(userId);
        row.setOperaterStatus(NumeralUtil.POSITIVE_TWO);
        districtMapper.updateByPrimaryKeySelective(row);
        return R.ok();
    }

    /**
     * @param id 1 编号
     * @return com.framework.common.model.response.R
     * @title 删除地区
     * @description 删除地区
     * @author 龘鵺
     * @datetime 2024-05-13 11:25:40
     * @version 1.0
     */
    @Override
    @Transactional
    public R<?> delete(Long id) {
        DistrictVo row = new DistrictVo();
        SystemUser systemUser = getUser();
        Long userId = systemUser.getId();
        Date date = new Date();
        row.setId(id);
        row.setOperaterTime(date);
        row.setOperaterId(userId);
        row.setOperaterStatus(NumeralUtil.NEGATIVE_ONE);
        districtMapper.delete(row);
        return R.ok();
    }

    /**
     * @param idList 1 编号集合
     * @return com.framework.common.model.response.R
     * @title 批量删除地区
     * @description 批量删除地区
     * @author 龘鵺
     * @datetime 2024-05-13 11:25:40
     * @version 1.0
     */
    @Override
    @Transactional
    public R<?> batchDeleteList(List<Long> idList) {
        DistrictVo row = new DistrictVo();
        SystemUser systemUser = getUser();
        Long userId = systemUser.getId();
        Date date = new Date();
        row.setIdList(idList);
        row.setOperaterTime(date);
        row.setOperaterId(userId);
        row.setOperaterStatus(NumeralUtil.NEGATIVE_ONE);
        //districtMapper.deleteList(row);
        return R.ok();
    }

    /**
     * @param row 1 地区类
     * @return java.util.List<com.framework.model.common.vo.DistrictVo>
     * @title 查询地区集合
     * @description 查询地区集合
     * @author 龘鵺
     * @datetime 2024-05-13 11:25:40
     * @version 1.0
     */
    @Override
    public List<DistrictVo> findByList(DistrictVo row) {
        row.setGtaeOperaterStatus(NumeralUtil.POSITIVE_ONE);
        List<DistrictVo> list = districtMapper.findByList(row);
        return list;
    }

    /**
     * @param row 1 地区类
     * @return com.framework.common.model.response.R
     * @title 分页查询地区列表
     * @description 分页查询地区列表
     * @author 龘鵺
     * @datetime 2024-05-13 11:25:40
     * @version 1.0
     */
    @Override
    public R<?> findParamPageList(DistrictVo row) {
        row.setGtaeOperaterStatus(NumeralUtil.POSITIVE_ONE);
        int count = districtMapper.findPageListCount(row);
        if (count == NumeralUtil.POSITIVE_ZERO) {
            return R.ok();
        }
        List<DistrictVo> list = districtMapper.findPageList(row);
        return R.ok(list, count);
    }
}